import { fileURLToPath, URL } from 'node:url'
import path, { resolve } from "path";
import { defineConfig, loadEnv } from 'vite'
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueSetupExtend from 'unplugin-vue-setup-extend-plus/vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Unocss from 'unocss/vite'
import ElementPlus from 'unplugin-element-plus/vite'
import { presetUno, presetAttributify, presetIcons } from 'unocss'
const outputHash = true
export default defineConfig(({ command, mode }) => {
    let env = loadEnv(mode, process.cwd());
    return {
        base: '/',
        server: {
            open: true,
            cors: true,
            host: env.VITE_APP_HOST,
            port: env.VITE_APP_PORT as unknown as number,
            proxy: {
                [env.VITE_APP_BASE_URL]: {
                    target: env.VITE_SERVE,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(new RegExp(`^${env.VITE_APP_BASE_URL}`), ""),
                },
            },
        },
        plugins: [
            vue(),
            vueJsx(),
            vueSetupExtend({}),
            Unocss({
                presets: [
                    presetUno(),
                    presetAttributify(),
                    presetIcons()],
            }),
            // 使用 svg 图标
            createSvgIconsPlugin({
                iconDirs: [resolve(process.cwd(), "src/assets/svg")],
                symbolId: "icon-[dir]-[name]"
            }),
            AutoImport({
                imports: ['vue', 'vue-router', 'pinia', '@vueuse/core'],
                resolvers: [ElementPlusResolver()],
            }),
            Components({
                resolvers: [ElementPlusResolver({ importStyle: "sass" })],
            }),
            ElementPlus({
                useSource: true,
            }),
        ],
        resolve: {
            alias: {
                '@': fileURLToPath(new URL('./src', import.meta.url)),
            }
        },
        css: {
            preprocessorOptions: {
                scss: {
                    additionalData: `@use "@/assets/style/var.scss" as *; `,
                },
            },
        },
        build: {
            assetsDir: 'static',
            // build时规定触发警告的 chunk 大小。（以 kbs 为单位）
            chunkSizeWarningLimit: 20480,
            // build时启用/禁用 CSS 代码拆分
            cssCodeSplit: true,
            // 生产环境构建文件的目录名
            outDir: 'dist',
            // 启用/禁用 gzip 压缩大小报告
            reportCompressedSize: false,
            rollupOptions: {
                onwarn: () => {
                    return
                },
                output: {
                    chunkFileNames: outputHash ? 'static/js/[name]-[hash].js' : 'static/js/[name].js',
                    entryFileNames: outputHash ? 'static/js/[name]-[hash].js' : 'static/js/[name].js',
                    assetFileNames: outputHash ? 'static/[ext]/[name]-[hash].[ext]' : 'static/[ext]/[name].[ext]',
                    manualChunks: {
                        'element-plus': ['element-plus'],
                    }
                }
            },
            // 混淆器 boolean | 'terser' | 'esbuild'
            minify: 'esbuild',
            target: 'es2015',
            sourcemap: false
        },
    }
})
